package com.wmh.baseservice.admin.controller;

import com.alibaba.fastjson2.JSON;
import com.wmh.baseservice.admin.entity.AdViewRecord;
import com.wmh.baseservice.admin.pojo.vo.viewRecord.AdViewRecordAddReqVO;
import com.wmh.baseservice.admin.pojo.vo.viewRecord.AdViewRecordUpdateReqVO;
import com.wmh.baseservice.admin.service.AdViewRecordService;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.common.utils.web.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @author 谢骏
 * @date 2023/7/16 13:49
 **/
@Api(tags = "用户-界面记录")
@RestController
@RequestMapping("admin/viewRecord")
@Validated
public class AdViewRecordController {

    @Resource
    private AdViewRecordService adViewRecordService;

    @Resource
    private UserUtil userUtil;

    @ApiOperation(value = "添加记录")
    @PostMapping("add")
    public String add(@Valid AdViewRecordAddReqVO reqVO) {
        adViewRecordService.add(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("delete")
    public String delete(
            @NotBlank(message = "未提交主键")
            @Size(min = 3, message = "未提交主键")
            String jsonIds
    ) {
        List<Long> ids = JSON.parseArray(jsonIds, Long.class);
        adViewRecordService.delete(ids);
        return ReturnBody.success();
    }

    @ApiOperation(value = "更新记录")
    @PostMapping("update")
    public String update(@Valid AdViewRecordUpdateReqVO reqVO) {
        adViewRecordService.update(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation(value = "根据用户和组件路径获取记录列表")
    @PostMapping("listByUserAndUrl")
    public String listByUserAndUrl(@NotBlank(message = "未提交组件url") String componentUrl,String name) {
        List<AdViewRecord> list = adViewRecordService.list(
                new LambdaQueryWrapperX<AdViewRecord>()
                        .eq(AdViewRecord::getAdId, userUtil.getId())
                        .eq(AdViewRecord::getComponentUrl, componentUrl)
                        .likeIfPresent(AdViewRecord::getName,name)
                        .select(AdViewRecord::getId,
                                AdViewRecord::getName,
                                AdViewRecord::getAdId,
                                AdViewRecord::getJsonParam,
                                AdViewRecord::getCreateTime,
                                AdViewRecord::getUpdateTime
                        )
                        .orderByDesc(AdViewRecord::getCreateTime)
        );
        return ReturnBody.success(list);
    }
}
